# import pandas as pd
#
# # 定义要合并的文件路径列表
# file_paths = [
#     r"C:\Users\20231112\Downloads\11.27\扶摇1.xlsx",
#     r"C:\Users\20231112\Downloads\11.27\扶摇2.xlsx",
#     r"C:\Users\20231112\Downloads\11.27\扶摇3.xlsx",
#     r"C:\Users\20231112\Downloads\11.27\扶摇4.xlsx"
# ]
#
# # 读取并合并所有文件
# data_frames = []
# for path in file_paths:
#     try:
#         df = pd.read_excel(path)
#         data_frames.append(df)
#     except Exception as e:
#         print(f"读取文件 {path} 失败: {e}")
#
# # 合并数据并筛选
# if data_frames:
#     combined_df = pd.concat(data_frames, ignore_index=True)
#
#     # 筛选日期为"全部"的记录
#     if '日期' in combined_df.columns:
#         filtered_df = combined_df[combined_df['日期'] == '全部']
#     else:
#         filtered_df = combined_df
#
#     # 只保留需要的列并去重
#     result_df = filtered_df[['抖音号名称', '抖音号ID']].drop_duplicates()
#
#     # 保存结果
#     result_df.to_excel(r'C:\Users\20231112\Downloads\11.27\投流消耗配置表\刘栋达人.xlsx', index=False)
#     print("合并完成！结果已保存到 扶摇合并结果.xlsx")

# V1.0

import pandas as pd
import os
from datetime import datetime

# 获取当前日期，格式为 MM.DD
current_date = datetime.now().strftime("%m.%d")

# 定义基于当前日期的基础路径
base_path = rf"C:\Users\20231112\Downloads\{current_date}"

# 定义文件路径列表
file_paths = [
    rf"{base_path}\扶摇1.xlsx",
    rf"{base_path}\扶摇2.xlsx",
    rf"{base_path}\扶摇3.xlsx",
    rf"{base_path}\扶摇4.xlsx"
]

# 确保输出目录存在
output_dir = rf"{base_path}\投流消耗配置表"
os.makedirs(output_dir, exist_ok=True)

# 读取并合并所有文件
data_frames = []
for path in file_paths:
    try:
        df = pd.read_excel(path)
        data_frames.append(df)
    except Exception as e:
        print(f"读取文件 {path} 失败: {e}")

# 合并数据并筛选
if data_frames:
    combined_df = pd.concat(data_frames, ignore_index=True)

    # 筛选日期为"全部"的记录
    if '日期' in combined_df.columns:
        filtered_df = combined_df[combined_df['日期'] == '全部']
    else:
        filtered_df = combined_df

    # 只保留需要的列并去重
    result_df = filtered_df[['抖音号名称', '抖音号ID']].drop_duplicates()

    # 保存结果到输出目录
    output_path = rf"{output_dir}\刘栋达人.xlsx"
    result_df.to_excel(output_path, index=False)
    print(f"合并完成！结果已保存到 {output_path}")
